//
//  GradientNode.swift
//  soapp_Example
//
//  Created by zhu xietong on 2018/3/1.
//  Copyright © 2018年 CocoaPods. All rights reserved.
//

import Foundation
import AsyncDisplayKit

class GradientNode: ASDisplayNode {
    open override class func draw(_ bounds: CGRect, withParameters parameters: Any?, isCancelled isCancelledBlock: () -> Bool, isRasterizing: Bool)
    {
        if let myContext = UIGraphicsGetCurrentContext()
        {
            myContext.saveGState()
            myContext.clip(to: bounds)
            
            let componentCount = 2;
            let zero:CGFloat = 0.0;let one:CGFloat = 1.0;
            let locations = [zero,one]
            let components = [zero, zero, zero, one, zero, zero, zero, zero]
            
            let myColorSpace = CGColorSpaceCreateDeviceRGB()
            let myGradient = CGGradient(colorSpace: myColorSpace, colorComponents: components, locations: locations, count: componentCount)
            
            let myStartPoint:CGPoint = CGPoint(x: bounds.midX, y: bounds.maxY)
            let myEndPoint = CGPoint(x: bounds.midX, y: bounds.midY)

            myContext.drawLinearGradient(myGradient!, start: myStartPoint, end: myEndPoint, options: .drawsAfterEndLocation)
            
            myContext.restoreGState()

        }
        
        
    }

}
